home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / mcga#01.zip / LESSON01.TXT < prev    next >
Text File  |  1992-06-11  |  5KB  |  183 lines

  1.                            MCGA Graphics Tutorial
  2.                                  Lesson #1
  3.                                 by Jim Cook
  4.  
  5. I'm not sure how this online tutorial will be received, but with your
  6. comments and feedback I plan on creating a full-blown animation package. This
  7. graphics library will be available to the public domain and will contain the
  8. following abilities:
  9.  
  10.                 Setting/Reading Pixels
  11.                 Drawing lines
  12.                 Saving/Restoring areas of the screen
  13.                 Displaying PCX/LBM files to the screen
  14.                 Spriting (Display picture with transparent areas)
  15.                 Palette control (Smooth fades to black)
  16.                 Page flipping
  17.  
  18. Before we're done, you will have the tools to produce programs with rich, 
  19. even photo-realistic (for the resolution) images on your PC.  The necessary 
  20. hardware is a VGA card and monitor that's it.  I'll be using Turbo Pascal
  21. version 6.0.  Please holler if that will be a problem.  I'm using it to 
  22. create inline assembly.  My alternatives are inline code (yuk) or linking in
  23. external assembly.  For speed (and actually ease) the latter is better.  If I
  24. receive three complaints against 6.0, I'll use external assembly.
  25.  
  26.                                 What is MCGA?
  27.  
  28. Multi-Color Graphics Array is the video card that IBM built into it's Model 
  29. 25 and 30 PS/2's.  It subsequently became a subset of the standard VGA 
  30. adapter card.  It has the distiction of being the first card (excluding 
  31. Targa and other expensive cards) to display 256 colors at once on the 
  32. computer screen.  To us that meant cool games and neat pictures.  The MCGA 
  33. addapter has added two new video modes to the PC world:
  34.  
  35.                 Mode $11        640x480x2 colors
  36.                 Mode $13        320x200x256 colors
  37.  
  38. Obviously, we will deal with mode $13.  If we wanted to deal with two 
  39. colors, we'd be programming a CGA.  So much for the history lesson...let's 
  40. dive in.
  41.  
  42. I've created a unit, MCGALib, that will contain all of our MCGA routines. 
  43. The first two procedures we will concern ourselves with are setting the
  44. graphics mode and setting a pixel.  The MCGALib is followed by a test 
  45. program that uses the two procedures:
  46.  
  47. Unit MCGALib;
  48.  
  49. interface
  50.  
  51. Procedure SetGraphMode (Num:Byte);
  52. Procedure SetPixel     (X,Y:Integer;Color:Byte);
  53.  
  54. implementation
  55.  
  56. var
  57.   ScreenWide  :  Integer;
  58.   ScreenAddr  :  Word;
  59.  
  60. Procedure SetGraphMode (Num:Byte);
  61. begin
  62.   asm
  63.     mov al,Num
  64.     mov ah,0
  65.     int 10h
  66.     end;
  67.   Case Num of
  68.     $13 : ScreenWide := 320;
  69.     end;
  70.   ScreenAddr := $A000;
  71. end;
  72. {
  73. Function PixelAddr (X,Y:Word) : Word;
  74. begin
  75.   PixelAddr := Y * ScreenWide + X;
  76. end;
  77.  
  78. Procedure SetPixel (X,Y:Integer;Color:Byte);
  79. var
  80.   Ofs    :  Word;
  81. begin
  82.   Ofs := PixelAddr (X,Y);
  83.   Mem [ScreenAddr:Ofs] := Color;
  84. end;
  85. }
  86.  
  87. Procedure SetPixel (X,Y:Integer;Color:Byte);
  88. begin
  89.   asm
  90.     push ds
  91.     mov  ax,ScreenAddr
  92.     mov  ds,ax
  93.  
  94.     mov  ax,Y
  95.     mov  bx,320
  96.     mul  bx
  97.     mov  bx,X
  98.     add  bx,ax
  99.  
  100.     mov  al,Color
  101.     mov  byte ptr ds:[bx],al
  102.     pop  ds
  103.     end;
  104. end;
  105.  
  106. Begin
  107. End.
  108.  
  109. This is the test program to make sure it's working...
  110.  
  111. Program MCGATest;
  112.  
  113. uses
  114.   Crt,Dos,MCGALib;
  115.  
  116. var
  117.   Stop,
  118.   Start  :  LongInt;
  119.   Regs   :  Registers;
  120.  
  121. Function Tick : LongInt;
  122. begin
  123.   Regs.ah := 0;
  124.   Intr ($1A,regs);
  125.   Tick := Regs.cx shl 16 + Regs.dx;
  126. end;
  127.  
  128. Procedure Control;
  129. var
  130.   I,J :  Integer;
  131. begin
  132.   Start := Tick;
  133.   For I := 0 to 199 do
  134.     For J := 0 to 320 do
  135.       SetPixel (J,I,Random(256));
  136.   Stop := Tick;
  137. end;
  138.  
  139. Procedure Closing;
  140. var
  141.   Ch     :  Char;
  142. begin
  143.   Repeat Until Keypressed;
  144.   While Keypressed do Ch := Readkey;
  145.   TextMode (3);
  146.   WriteLn ('Routine took ',(Stop-Start),' ticks or ',(Stop-Start)/18.2:4:3,'
  147.  seconds!');
  148. end;
  149.  
  150. Procedure Init;
  151. begin
  152.   SetGraphMode ($13);
  153.   Randomize;
  154. end;
  155.  
  156. Begin
  157.   Init;
  158.   Control;
  159.   Closing;
  160. End.
  161.  
  162. OK, I can see where these listings could get unbearably long in time.  I'll 
  163. explore a few ways I can get this information to ya'll without taking up too 
  164. much space.  I would like you to make sure this routine works, just in case 
  165. you had questions about your graphics card.  You may notice two SetPixel 
  166. procedures in the MCGALib, one is commented out.  Remove the comments, 
  167. comment up the uncommented SetPixel and run the test program again.  Notice 
  168. the speed degradation.  Linking in raw assembly will even improve upon the 
  169. speed of the inline assembly.
  170.  
  171. Please take the time to study each procedure and ASK ANY QUESTIONS that you 
  172. may have, even if it doesn't relate to the graphics routines.  I'm certain I 
  173. do not want to get pulled off track by any discussions about STYLE, but 
  174. please post your critique for others to learn from.
  175.  
  176.                               Coming next time
  177.  
  178. I think a discussion of video memory is paramount.  Possibly vertical and
  179. horizontal lines, if space permits.
  180.  
  181. Happy grafx
  182. jim  
  183.